let reqJs = require('../../utils/req.js')

Component({
    properties: {
        sid: String, // 站点ID
        pid: { // 项目ID
            type: Number,
            value: 0
        },
        maxCount: {
            type: Number,
            value: 1
        }
    },

    data: {
        isUploading: false,
        uploadProgress: 0,
        errorMessage: '',
        uploadedImages: [] // 存储已上传图片的URL
    },

    methods: {
        // 选择图片并直接上传
        chooseAndUploadImage() {
            const that = this;
            const {
                maxCount,
                sid,
                pid
            } = this.data;

            wx.chooseImage({
                count: maxCount,
                sizeType: ['compressed'], // 压缩图
                sourceType: ['album', 'camera'],
                success: (res) => {
                    that.setData({
                        isUploading: true,
                        uploadProgress: 0,
                        errorMessage: ''
                    });

                    // 开始上传第一张图片
                    that.uploadImage(res.tempFilePaths[0], sid, pid);
                },
                fail: (err) => {
                    console.error('选择图片失败', err);
                    that.setData({
                        errorMessage: '选择图片失败，请重试'
                    });
                }
            });
        },

        // 上传图片到服务器
        uploadImage(tempFilePath, sid, pid) {
            const that = this;

            // 读取文件为base64
            wx.getFileSystemManager().readFile({
                filePath: tempFilePath,
                encoding: 'base64',
                success: (res) => {
                    // 提取base64数据（去掉前缀）
                    const base64Data = res.data.split(',')[1] || res.data;



                    // 更新上传进度
                    that.setData({
                        uploadProgress: 30
                    });

                    reqJs.uploadImg(pid, base64Data).then(res => {

                        that.setData({
                            uploadProgress: 100
                        });

                        if (res.code === 200) {

                            // 上传成功，保存图片URL
                            const newImages = [res.data];
                            that.setData({
                                // uploadedImages: newImages,
                                isUploading: false
                            });

                            // 这里还需要去调用打卡接口

                            // return

                            console.log('>>>>>')
                            reqJs.playGame({
                                pid: pid,
                                option: newImages[0]
                            }).then(res => {

                                console.log('>>>>>', res)
                                if (res.code != 200) {
                                    return
                                }
                                wx.showToast({
                                    title: '上传成功',
                                    icon: 'success'
                                });
                                that.setData({
                                    uploadedImages: newImages,
                                    isUploading: false
                                });
                                // 触发上传成功事件
                                that.triggerEvent('uploadSuccess', {
                                    url: res.data
                                });


                            }).catch(error => {
                                console.log(error)
                            })


                        } else {
                            that.setData({
                                errorMessage: res.data.msg || '上传失败',
                                isUploading: false
                            });
                        }
                    }).catch(error => {
                        console.error('上传失败', err);
                        that.setData({
                            errorMessage: '上传失败，请检查网络',
                            isUploading: false
                        });
                    })


                },
                fail: (err) => {
                    console.error('读取文件失败', err);
                    that.setData({
                        errorMessage: '图片处理失败',
                        isUploading: false
                    });
                }
            });
        },

        // 预览已上传的图片
        previewUploadedImages() {
            // const {
            //     uploadedImages
            // } = this.data;

            // if (uploadedImages.length === 0) {
            //     wx.showToast({
            //         title: '请先上传图片',
            //         icon: 'none'
            //     });
            //     return;
            // }

            // wx.previewImage({
            //     current: uploadedImages[0],
            //     urls: uploadedImages
            // });




            reqJs.getUserProjectInfo({
                pid: this.data.pid,
            }).then(res => {

                console.log('>>>>>', res)
                if (res.code != 200) {
                    return
                }

                if (res.data.option == "") {
                    wx.showToast({
                        title: '请先上传图片',
                        icon: 'none'
                    });
                    return;
                }

                wx.previewImage({
                    urls: [res.data.option],
                    current: res.data.option
                });



            }).catch(error => {
                console.log(error)
            })
        }
    }
});